/**
 * 向流程事件关联人员发送消息
 */
def sendTriggerMessage(txt, receiver=null, triggerDataContext=__TRIGGER_CONTEXT__){
	
	try {
		
		if(snail.common.utils.StringHelper.isBlank(receiver)) {
			
			if(__IS_TRIGGER__) {
				
				com.seeyon.apps.snail.common.MessageHepler.send(txt, [triggerDataContext.event.userId] as long[]);
				
			}else {
				
				com.seeyon.apps.snail.common.MessageHepler.send(txt, [com.seeyon.ctp.common.AppContext.currentUserId()] as long[]);
			}
			
		}else {
			sendMessage(txt, receiver);
		}
		
	} catch(Throwable e){
		
		logger("向流程事件关联人员发送消息失败！[TXT: ${txt}, ERROR: ${e.message}]");
	}
}

/**
 * 获取触发器配置信息【注：不建议外部使用】
 */
def _getTriggerConfigInfo(key, config) {
	
	def value = {listData->
	
		for(data in listData) {
			
			def type = data.get('type');
			
			if('object' == type || 'array' == type) {
				
				def value = call(data.children);
				
				if(value[0]) {
					return value;
				}
				
				continue;
			}
			
			if(key == data.key) {
				
				return [true, data];
			}
		}
		
		return [false, null];
		
	}(config);
	
	
	return value[0] ? value[1] : null;
}


/**
 * 获取触发器配置值 【注：不建议外部使用】
 */
def _getTriggerConfigValue(key, defaultValue, config) {
	
	def value = {listData->
	
		for(data in listData) {
			
			def type = data.get('type');
			
			if('object' == type || 'array' == type) {
				
				def value = call(data.children);
				
				if(value[0]) {
					return value;
				}
				
				continue;
			}
			
			if(key == data.key) {
				
				def inKey = data.get('inKey');
				
				if(StringHelper.isBlank(inKey)) {
					
					return [true, data.get('defaultValue')];
				}
				
				inKey = inKey.substring(inKey.indexOf('.') + 1);

				def value = null;
				
				if(null == type || 'string' == type) {
					
					value = getString(inKey);
					
				}else if('number' == type) {
					
					value = getNumber(inKey);
					
				}else if('date' == type) {
					
					value = getDateText(inKey);
					
				}else if('datetime' == type) {
					
					value = getTimeText(inKey);
					
				}else if('enum' == type) {
					
					value = getEnumShowvalue(inKey);
					
				}else if('enum_value' == type) {
					
					value = getEnumValue(inKey);
					
				}else if('enum_code' == type) {
					
					value = getEnumCode(inKey);
					
				}else if('member' == type) {
					
					value = getMember(inKey);
					
				}else if('member_code' == type) {
					
					value = getMemberCode(inKey);
					
				}else if('member_loginname' == type) {
					
					value = getLoginName(inKey);
					
				}else if('member_telnumber' == type) {
					
					value = getTelNumber(inKey);
					
				}else if('member_officenum' == type) {
					
					value = getOfficeNum(inKey);
					
				}else if('member_email' == type) {
					
					value = getEmailAddress(inKey);
					
				}else if('member_idnum' == type) {
					
					value = getMemberIDNUM(inKey);
					
				}else if('department' == type) {
					
					value = getDeptName(inKey);
					
				}else if('department_code' == type) {
					
					value = getDeptCode(inKey);
					
				}else if('company' == type) {
					
					value = getUnitName(inKey);
					
				}else if('company_code' == type) {
					
					value = getUnitCode(inKey);	
					
				}else if('post' == type) {
					
					value = getPostName(inKey);
					
				}else if('post_code' == type) {
					
					value = getPostCode(inKey);		
					
				}else if('level' == type) {
					
					value = getLevelName(inKey);
					
				}else if('level_code' == type) {
					
					value = getLevelCode(inKey);	
					
				}else {
					value = getValue(inKey);
				}
				
				return [true, (StringHelper.isBlank(value) ? data.get('defaultValue') : value)];
			}
		}
		
		return [false, null];
		
	}(config);
	
	
	return value[0] ? value[1] : defaultValue;
}

/**
 * 获取触发器的业务数据对象
 */
def getBusinessBillsData(mergeConfig=null, config=__TRIGGER_CONFIG__) {
	
	def businessData = [:];
	def businessDataType = [:];
	
	{listData, mapData, mapDataType->
			
		for(data in listData) {
			
			def value = null;
			def type = data.get('type');
			
			mapDataType[data.key] = type;
			
			if('object' == type || 'array' == type) {
				
				call(data.children, mapData[data.key]=[:], mapDataType[data.key+'_type']=[:]);

				continue;
			}

			
			def inKey = data.get('inKey');
			
			if(StringHelper.isBlank(inKey)) {
				
				mapData[data.key] = data.get('defaultValue');;
				
				continue;
			}
			
			inKey = inKey.substring(inKey.indexOf('.') + 1);
			
			
			if(null == type || 'string' == type) {
				
				value = getString(inKey);
				
			}else if('number' == type) {
				
				value = getNumber(inKey);
				
			}else if('date' == type) {
				
				value = getDateText(inKey);
				
			}else if('datetime' == type) {
				
				value = getTimeText(inKey);
				
			}else if('enum' == type) {
				
				value = getEnumShowvalue(inKey);
				
			}else if('enum_value' == type) {
				
				value = getEnumValue(inKey);
				
			}else if('enum_code' == type) {
				
				value = getEnumCode(inKey);
				
			}else if('member' == type) {
				
				value = getMember(inKey);
				
			}else if('member_code' == type) {
				
				value = getMemberCode(inKey);
				
			}else if('member_loginname' == type) {
				
				value = getLoginName(inKey);
				
			}else if('member_telnumber' == type) {
				
				value = getTelNumber(inKey);
				
			}else if('member_officenum' == type) {
				
				value = getOfficeNum(inKey);
				
			}else if('member_email' == type) {
				
				value = getEmailAddress(inKey);
				
			}else if('member_idnum' == type) {
				
				value = getMemberIDNUM(inKey);
				
			}else if('department' == type) {
				
				value = getDeptName(inKey);
				
			}else if('department_code' == type) {
				
				value = getDeptCode(inKey);
				
			}else if('company' == type) {
				
				value = getUnitName(inKey);
				
			}else if('company_code' == type) {
				
				value = getUnitCode(inKey);
				
			}else if('post' == type) {
				
				value = getPostName(inKey);
				
			}else if('post_code' == type) {
				
				value = getPostCode(inKey);		
				
			}else if('level' == type) {
				
				value = getLevelName(inKey);
				
			}else if('level_code' == type) {
				
				value = getLevelCode(inKey);	
				
			}else {
				
				value = getValue(inKey);
			}
			
			mapData[data.key] = (StringHelper.isBlank(value) ? data.get('defaultValue') : value);
		}
		
	}(config.fieldInfo, businessData, businessDataType);
	
	
	// 拆解数据
	def disassembleData = {mapData->

		def size = 0;
		
		for(e in mapData) {
			
			if(e.value instanceof java.util.List) {
				
				size = (size < e.value.size() ? e.value.size() : size);
			}
		}
		
		def newData = null;
		
		if(0 < size) {
			
			newData = [];
			
			(0..<size).each{i->
			
				newData.add([:]);
			
				({_mapData, _newMapData, _i->
				
					_mapData.each{key,value->
					
						if(value instanceof java.util.List) {
							
							_newMapData[key] = (_i < value.size() ? value[_i] : null);
							
						}else {
							_newMapData[key] = value;
						}
					}
				
				})(mapData, newData[-1], i);
			}
		} else {
			newData = [mapData];
		}
		
		return newData;
	}
	

	
	def businessDatas = [];
	
	{mapData, mapDataType, listData->

		listData.addAll(disassembleData(mapData));
	
		for(data in listData) {
			
			for(k in data.keySet()) {
				
				if(data[k] instanceof java.util.Map) {
									
					call(data[k], mapDataType[k+'_type'], data[k]=[]);
					
					if('object' == mapDataType[k]) {
						
						if(0 < data[k].size()) {
							data[k] = data[k][0];
						}
					}
				}
			}
		}	
	}(businessData, businessDataType, businessDatas);
	
	// 合并
	if(null != mergeConfig) {
		
		
		mergeConfig.each{mergeKey, mergeInfo->
			
			// 1. 去多余数据
			{_mergeKey, _mergeInfo, listData->
				
				for(data in listData) {
					
					for(mainData in data[_mergeKey]) {
						
						def itemData = mainData[_mergeInfo.sub];
						
						for(def i=0; i<itemData.size(); i++) {
							
							def matching = true;
							
							for(mpg in _mergeInfo.mapping) {
								
								if((mainData.get(mpg[0]) as String) != (itemData[i].get(mpg[1]) as String)) {
									matching = false;
									break;
								}
							}
							
							if(matching) {
								continue;
							}
							
							itemData.remove(i);
							
							i--;
						}
					}
				}	
			}(mergeKey, mergeInfo, businessDatas);
			
			// 2. 合并
			{_mergeKey, _mergeInfo, listData->
			
				for(data in listData) {
					
					def itemsData = data[_mergeKey];
					
					for(def i=0; i<itemsData.size(); i++) {
						
						for(def j=i+1; j<itemsData.size(); j++) {
							
							def matching = true;
							
							for(mpg in _mergeInfo.mapping) {
								
								if((itemsData[i].get(mpg[0]) as String) != (itemsData[j].get(mpg[0]) as String)) {
									matching = false;
									break;
								}
							}
							
							if(!matching) {
								continue;
							}
																
							itemsData.remove(j);
							
							j--;
						}
					}
				}	
			}(mergeKey, mergeInfo, businessDatas);
			
		}		
	}

	if(0 == businessDatas.size()) {
		
		return null;
		
	}else if(1 == businessDatas.size()) {
		
		return businessDatas[0];
	}
	
	return businessDatas;
}



/**
 * 获取触发器配置值
 */
def getBusinessAttributeValue(key, defaultValue=null, config=__TRIGGER_CONFIG__) {
	
	return _getTriggerConfigValue(key, defaultValue, config.attributeInfo);
}


/**
 * 更新表单中业务配置数据（数据表）
 */
def setBusinessAttributeData(key, value, sleeptime=200, config=__TRIGGER_CONFIG__) {
	
	def fieldInfo = _getTriggerConfigInfo(key, config.attributeInfo);
	
	if(null == fieldInfo) {
		
		logger("在触发器配置中，${key}不存在！");
		
		return null;
	}
	
	def inKey = fieldInfo.get('inKey');
	
	if(null == inKey) {
		
		logger("在触发器配置中，${key}无配置值！");
		
		return null;
	}
	
	setValue(inKey.substring(inKey.indexOf('.') + 1), value, sleeptime);
}



/* 从【触发器/流程事件】的环境对象中获取表单数据，并转换为时间 */
def getTime(key, defaultValue=null, triggerDataContext=__TRIGGER_CONTEXT__){
    
    def value = null;
    
	try {
		
		value = getValue(key, null, triggerDataContext);
		
		if(null == value){
			return defaultValue;
		}
		
		if(value instanceof java.util.List){
			
			(0..<value.size()).each{i->
				
				try {
					
					if(value[i] instanceof String){
						value[i] = new java.sql.Timestamp(snail.common.utils.DateHelper.parse(value[i], 'yyyy-MM-dd HH:mm:ss').getTime());
					}
				} catch(Exception e){}
			}
			
		} else if(value instanceof String){
			value = new java.sql.Timestamp(snail.common.utils.DateHelper.parse(value, 'yyyy-MM-dd HH:mm:ss').getTime());
		}

	} catch(Exception e){
		logger("从【${__IS_TRIGGER__ ? '触发器': '流程事件'}】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
	}
	
    return value;
}


/* 从【触发器/流程事件】的环境对象中获取表单数据，并转换为时间文本 */
def getTimeText(key, defaultValue=null, triggerDataContext=__TRIGGER_CONTEXT__){
    
    def value = null;
    
	try {
		
		value = getValue(key, null, triggerDataContext);
		
		if(null == value){
			return defaultValue;
		}
		
		if(value instanceof java.util.List){
			
			(0..<value.size()).each{i->
				try {
					value[i] = ((null != value[i]) ? (new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(value[i])) : '');
				} catch(Exception e){}
			}
			
		} else if(value instanceof java.util.Date){
			value = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(value);
		}

	} catch(Exception e){
		logger("从【${__IS_TRIGGER__ ? '触发器': '流程事件'}】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
	}
	
    return value;
}


/* 从【触发器/流程事件】的环境对象中获取表单数据，并转换为日期 */
def getDate(key, defaultValue=null, triggerDataContext=__TRIGGER_CONTEXT__){
    
    def value = null;
    
	try {
		
		value = getValue(key, null, triggerDataContext);
		
		if(null == value){
			return defaultValue;
		}
		
		if(value instanceof java.util.List){
			
			(0..<value.size()).each{i->
				
				try {
					
					if(value[i] instanceof String){
						value[i] = snail.common.utils.DateHelper.parse(value[i]);
					}
				} catch(Exception e){}
			}
			
		} else if(value instanceof String){
			value = snail.common.utils.DateHelper.parse(value);
		}

	} catch(Exception e){
		logger("从【${__IS_TRIGGER__ ? '触发器': '流程事件'}】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
	}
	
    return value;
}


/* 从【触发器/流程事件】的环境对象中获取表单数据，并转换为日期文本 */
def getDateText(key, defaultValue=null, triggerDataContext=__TRIGGER_CONTEXT__){
    
    def value = null;
    
	try {
		
		value = getValue(key, null, triggerDataContext);
		
		if(null == value){
			return defaultValue;
		}
		
		if(value instanceof java.util.List){
			
			(0..<value.size()).each{i->
				try {
					value[i] = ((null != value[i]) ? (new java.text.SimpleDateFormat("yyyy-MM-dd").format(value[i])) : '');
				} catch(Exception e){}
			}
			
		} else if(value instanceof java.util.Date){
			value = new java.text.SimpleDateFormat("yyyy-MM-dd").format(value);
		}

	} catch(Exception e){
		logger("从【${__IS_TRIGGER__ ? '触发器': '流程事件'}】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
	}
	
    return value;
}


/* 从【触发器/流程事件】的环境对象中获取表单数据，并转换为枚举显示值 */
def getEnumShowvalue(key, defaultValue=null, triggerDataContext=__TRIGGER_CONTEXT__){
    
    try {
        
        def enumManager = com.seeyon.ctp.common.AppContext.getBean("enumManagerNew");
        
        def id = getValue(key, defaultValue, triggerDataContext);
        
        if(null == id){
            return defaultValue;
        }
        
        if(id instanceof java.util.List){
            
            def names = [];
            
            (0..<id.size()).each{i->
            
                try {
                    def enumItem = enumManager.getEnumItem(new Long(id[i] as String));
                
                    names << ((null != enumItem) ? enumItem.getShowvalue() : '');
                } catch(Exception e){
                    names << '';
                }
            }
            
            return names;
            
        } else {
            def enumItem = enumManager.getEnumItem(new Long(id as String));
            
            return ((null != enumItem) ? enumItem.getShowvalue() : defaultValue);    
        }

    } catch(Exception e){
        logger("从【${__IS_TRIGGER__ ? '触发器': '流程事件'}】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
    }
    
    return defaultValue;
}


/* 从【触发器/流程事件】的环境对象中获取表单数据，并转换为枚举值 */
def getEnumValue(key, defaultValue=null, triggerDataContext=__TRIGGER_CONTEXT__){
    
    try {

        def enumManager = com.seeyon.ctp.common.AppContext.getBean("enumManagerNew");
        
        def id = getValue(key, defaultValue, triggerDataContext);
        
        if(null == id){
            return defaultValue;
        }
        
        if(id instanceof java.util.List){
            
            def names = [];
            
            (0..<id.size()).each{i->
                
                try {
                    def enumItem = enumManager.getEnumItem(new Long(id[i] as String));
                    
                    names << ((null != enumItem) ? enumItem.getEnumvalue() : '');
                } catch(Exception e){
                    names << '';
                }
                
            }
            
            return names;
            
        } else {
            def enumItem = enumManager.getEnumItem(new Long(id as String));
            
            return ((null != enumItem) ? enumItem.getEnumvalue() : defaultValue);    
        }
        
    } catch(Exception e){
        logger("从【${__IS_TRIGGER__ ? '触发器': '流程事件'}】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
    }
    
    return defaultValue;
}


/* 从【触发器/流程事件】的环境对象中获取表单数据，并转换为枚举编码 */
def getEnumCode(key, defaultValue=null, triggerDataContext=__TRIGGER_CONTEXT__){
    
    try {

        def enumManager = com.seeyon.ctp.common.AppContext.getBean("enumManagerNew");
        
        def id = getValue(key, defaultValue, triggerDataContext);
        
        if(null == id){
            return defaultValue;
        }
        
        if(id instanceof java.util.List){
            
            def names = [];
            
            (0..<id.size()).each{i->
                
                try {
                    def enumItem = enumManager.getEnumItem(new Long(id[i] as String));
                    
                    names << ((null != enumItem) ? enumItem.getEnumItemCode() : '');
                } catch(Exception e){
                    names << '';
                }
                
            }
            
            return names;
            
        } else {
            def enumItem = enumManager.getEnumItem(new Long(id as String));
            
            return ((null != enumItem) ? enumItem.getEnumItemCode() : defaultValue);    
        }
        
    } catch(Exception e){
        logger("从【${__IS_TRIGGER__ ? '触发器': '流程事件'}】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
    }
    
    return defaultValue;
}


/* 从【触发器/流程事件】的环境对象中获取表单数据，并转换为姓名 */
def getMember(key, defaultValue=null, triggerDataContext=__TRIGGER_CONTEXT__){
    
    try {

        def orgManager = com.seeyon.ctp.common.AppContext.getBean("orgManager");
        
        def id = getValue(key, defaultValue, triggerDataContext);
        
        if(null == id){
            return defaultValue;
        }
        
        if(id instanceof java.util.List){
            
            def names = [];
            
            (0..<id.size()).each{i->
            
                def _names = [];
                
                try {
                    
                    id[i] = ((null != id[i]) ? (id[i] as String).split(',') : '');
                    
                    id[i].each{
                        
                        try {
                            def member = orgManager.getMemberById(new Long(it));
                        
                            _names << ((null != member) ? member.name : '');
                        } catch(Exception e){}
                    }
                    
                    names << _names.join(',');
                } catch(Exception e){
                    names << '';
                }
            }
            
            return names;
        } else {
            
            def names = [];
            
            id = (id as String).split(',');
            
            id.each{
                
                def member = orgManager.getMemberById(new Long(it));
                
                names << ((null != member) ? member.name : '');
            }
            
            return names.join(',');
        }
        
    } catch(Exception e){
        logger("从【${__IS_TRIGGER__ ? '触发器': '流程事件'}】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
    }
    
    return defaultValue;
}


/* 从【触发器/流程事件】的环境对象中获取表单数据，并转换为人员编号 */
def getMemberCode(key, defaultValue=null, triggerDataContext=__TRIGGER_CONTEXT__){
    
    try {

        def orgManager = com.seeyon.ctp.common.AppContext.getBean("orgManager");
        
        def id = getValue(key, defaultValue, triggerDataContext);
        
        if(null == id){
            return defaultValue;
        }
        
        if(id instanceof java.util.List){
            
            def names = [];
            
            (0..<id.size()).each{i->
            
                def _values = [];
                
                try {
                    
                    id[i] = ((null != id[i]) ? (id[i] as String).split(',') : '');
                    
                    id[i].each{
                        
                        try {
                            def member = orgManager.getMemberById(new Long(it));
                        
                            _values << ((null != member) ? member.code : '');
                        } catch(Exception e){}
                    }
                    
                    names << _values.join(',');
                } catch(Exception e){
                    names << '';
                }
            }
            
            return names;
        } else {
            
            def names = [];
            
            id = (id as String).split(',');
            
            id.each{
                
                def member = orgManager.getMemberById(new Long(it));
                
                names << ((null != member) ? member.code : '');
            }
            
            return names.join(',');
        }
        
    } catch(Exception e){
        logger("从【${__IS_TRIGGER__ ? '触发器': '流程事件'}】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
    }
    
    return defaultValue;
}


/* 从【触发器/流程事件】的环境对象中获取表单数据，并转换为人员登录名 */
def getLoginName(key, defaultValue=null, triggerDataContext=__TRIGGER_CONTEXT__){
    
    try {

        def orgManager = com.seeyon.ctp.common.AppContext.getBean("orgManager");
        
        def id = getValue(key, defaultValue, triggerDataContext);
        
        if(null == id){
            return defaultValue;
        }
        
        if(id instanceof java.util.List){
            
            def names = [];
            
            (0..<id.size()).each{i->
            
                def _values = [];
                
                try {
                    
                    id[i] = ((null != id[i]) ? (id[i] as String).split(',') : '');
                    
                    id[i].each{
                        
                        try {
                            def member = orgManager.getMemberById(new Long(it));
                        
                            _values << ((null != member) ? member.loginName : '');
                        } catch(Exception e){}
                    }
                    
                    names << _values.join(',');
                } catch(Exception e){
                    names << '';
                }
            }
            
            return names;
        } else {
            
            def names = [];
            
            id = (id as String).split(',');
            
            id.each{
                
                def member = orgManager.getMemberById(new Long(it));
                
                names << ((null != member) ? member.loginName : '');
            }
            
            return names.join(',');
        }
        
    } catch(Exception e){
        logger("从【${__IS_TRIGGER__ ? '触发器': '流程事件'}】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
    }
    
    return defaultValue;
}


/* 从【触发器/流程事件】的环境对象中获取表单数据，并转换为人员的手机号码 */
def getTelNumber(key, defaultValue=null, triggerDataContext=__TRIGGER_CONTEXT__){
    
    try {

        def orgManager = com.seeyon.ctp.common.AppContext.getBean("orgManager");
        
        def id = getValue(key, defaultValue, triggerDataContext);
        
        if(null == id){
            return defaultValue;
        }
        
        if(id instanceof java.util.List){
            
            def names = [];
            
            (0..<id.size()).each{i->
            
                def _values = [];
                
                try {
                    
                    id[i] = ((null != id[i]) ? (id[i] as String).split(',') : '');
                    
                    id[i].each{
                        
                        try {
                            def member = orgManager.getMemberById(new Long(it));
                        
                            _values << ((null != member) ? member.telNumber : '');
                        } catch(Exception e){}
                    }
                    
                    names << _values.join(',');
                } catch(Exception e){
                    names << '';
                }
            }
            
            return names;
        } else {
            
            def names = [];
            
            id = (id as String).split(',');
            
            id.each{
                
                def member = orgManager.getMemberById(new Long(it));
                
                if(null != member && snail.common.utils.StringHelper.isNotBlank(member.telNumber)) {
                	
                	names << member.telNumber;
                }
            }
            
            return names.join(',');
        }
        
    } catch(Exception e){
        logger("从【${__IS_TRIGGER__ ? '触发器': '流程事件'}】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
    }
    
    return defaultValue;
}


/* 从【触发器/流程事件】的环境对象中获取表单数据，并转换为人员的办公电话 */
def getOfficeNum(key, defaultValue=null, triggerDataContext=__TRIGGER_CONTEXT__){
    
    try {

        def orgManager = com.seeyon.ctp.common.AppContext.getBean("orgManager");
        
        def id = getValue(key, defaultValue, triggerDataContext);
        
        if(null == id){
            return defaultValue;
        }
        
        if(id instanceof java.util.List){
            
            def names = [];
            
            (0..<id.size()).each{i->
            
                def _values = [];
                
                try {
                    
                    id[i] = ((null != id[i]) ? (id[i] as String).split(',') : '');
                    
                    id[i].each{
                        
                        try {
                            def member = orgManager.getMemberById(new Long(it));
                        
                            _values << ((null != member) ? member.officeNum : '');
                        } catch(Exception e){}
                    }
                    
                    names << _values.join(',');
                } catch(Exception e){
                    names << '';
                }
            }
            
            return names;
        } else {
            
            def names = [];
            
            id = (id as String).split(',');
            
            id.each{
                
                def member = orgManager.getMemberById(new Long(it));
                
                if(null != member && snail.common.utils.StringHelper.isNotBlank(member.officeNum)) {
                	
                	names << member.officeNum;
                }
            }
            
            return names.join(',');
        }
        
    } catch(Exception e){
        logger("从【${__IS_TRIGGER__ ? '触发器': '流程事件'}】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
    }
    
    return defaultValue;
}


/* 从【触发器/流程事件】的环境对象中获取表单数据，并转换为人员的Email */
def getEmailAddress(key, defaultValue=null, triggerDataContext=__TRIGGER_CONTEXT__){
    
    try {

        def orgManager = com.seeyon.ctp.common.AppContext.getBean("orgManager");
        
        def id = getValue(key, defaultValue, triggerDataContext);
        
        if(null == id){
            return defaultValue;
        }
        
        if(id instanceof java.util.List){
            
            def names = [];
            
            (0..<id.size()).each{i->
            
                def _values = [];
                
                try {
                    
                    id[i] = ((null != id[i]) ? (id[i] as String).split(',') : '');
                    
                    id[i].each{
                        
                        try {
                            def member = orgManager.getMemberById(new Long(it));
                        
                            _values << ((null != member) ? member.emailAddress : '');
                        } catch(Exception e){}
                    }
                    
                    names << _values.join(',');
                } catch(Exception e){
                    names << '';
                }
            }
            
            return names;
        } else {
            
            def names = [];
            
            id = (id as String).split(',');
            
            id.each{
                
                def member = orgManager.getMemberById(new Long(it));
                
                if(null != member && snail.common.utils.StringHelper.isNotBlank(member.emailAddress)) {
                	
                	names << member.emailAddress;
                }
            }
            
            return names.join(',');
        }
        
    } catch(Exception e){
        logger("从【${__IS_TRIGGER__ ? '触发器': '流程事件'}】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
    }
    
    return defaultValue;
}


/* 从【触发器/流程事件】的环境对象中获取表单数据，并转换为人员的身份证号 */
def getMemberIDNUM(key, defaultValue=null, triggerDataContext=__TRIGGER_CONTEXT__){
    
    try {

        def orgManager = com.seeyon.ctp.common.AppContext.getBean("orgManager");
        
        def id = getValue(key, defaultValue, triggerDataContext);
        
        if(null == id){
            return defaultValue;
        }
        
        if(id instanceof java.util.List){
            
            def names = [];
            
            (0..<id.size()).each{i->
            
                def _values = [];
                
                try {
                    
                    id[i] = ((null != id[i]) ? (id[i] as String).split(',') : '');
                    
                    id[i].each{
                        
                        try {
                            def member = orgManager.getMemberById(new Long(it));
                        
                            _values << ((null != member) ? member.idNum : '');
                        } catch(Exception e){}
                    }
                    
                    names << _values.join(',');
                } catch(Exception e){
                    names << '';
                }
            }
            
            return names;
        } else {
            
            def names = [];
            
            id = (id as String).split(',');
            
            id.each{
                
                def member = orgManager.getMemberById(new Long(it));
                
                if(null != member && snail.common.utils.StringHelper.isNotBlank(member.idNum)) {
                	
                	names << member.idNum;
                }
            }
            
            return names.join(',');
        }
        
    } catch(Exception e){
        logger("从【${__IS_TRIGGER__ ? '触发器': '流程事件'}】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
    }
    
    return defaultValue;
}


/* 从【触发器/流程事件】的环境对象中获取表单数据，并转换为部门名称 */
def getDeptName(key, defaultValue=null, triggerDataContext=__TRIGGER_CONTEXT__){

	try {

		def orgManager = com.seeyon.ctp.common.AppContext.getBean("orgManager");
		
		def id = getValue(key, defaultValue, triggerDataContext);
		
		if(null == id){
			return defaultValue;
		}
		
		if(id instanceof java.util.List){
			
			def names = [];
			
			(0..<id.size()).each{i->
			
				def _names = [];
				
				try {
					
					id[i] = ((null != id[i]) ? (id[i] as String).split(',') : '');
					
					id[i].each{
						
						try {
							def department = orgManager.getDepartmentById(new Long(it));
						
							_names << ((null != department) ? department.name : '');
						} catch(Exception e){}
					}
					
					names << _names.join(',');
				} catch(Exception e){
					names << '';
				}
			}
			
			return names;
		} else {
			
			def names = [];
			
			id = (id as String).split(',');
			
			id.each{
				
				def department = orgManager.getDepartmentById(new Long(it));
				
				names << ((null != department) ? department.name : '');
			}
			
			return names.join(',');
		}
		
	} catch(Exception e){
		logger("从【${__IS_TRIGGER__ ? '触发器': '流程事件'}】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
	}
    
    return defaultValue;
}

/* 从【触发器/流程事件】的环境对象中获取表单数据，并转换为部门编号 */
def getDeptCode(key, defaultValue=null, triggerDataContext=__TRIGGER_CONTEXT__){

	try {

		def orgManager = com.seeyon.ctp.common.AppContext.getBean("orgManager");
		
		def id = getValue(key, defaultValue, triggerDataContext);
		
		if(null == id){
			return defaultValue;
		}
		
		if(id instanceof java.util.List){
			
			def names = [];
			
			(0..<id.size()).each{i->
			
				def _values = [];
				
				try {
					
					id[i] = ((null != id[i]) ? (id[i] as String).split(',') : '');
					
					id[i].each{
						
						try {
							def department = orgManager.getDepartmentById(new Long(it));
						
							_values << ((null != department) ? department.code : '');
						} catch(Exception e){}
					}
					
					names << _values.join(',');
				} catch(Exception e){
					names << '';
				}
			}
			
			return names;
		} else {
			
			def names = [];
			
			id = (id as String).split(',');
			
			id.each{
				
				def department = orgManager.getDepartmentById(new Long(it));
				
				names << ((null != department) ? department.code : '');
			}
			
			return names.join(',');
		}
		
	} catch(Exception e){
		logger("从【${__IS_TRIGGER__ ? '触发器': '流程事件'}】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
	}
    
    return defaultValue;
}

/* 从【触发器/流程事件】的环境对象中获取表单数据，并转换为单位名称 */
def getUnitName(key, defaultValue=null, triggerDataContext=__TRIGGER_CONTEXT__){

	try {

		def orgManager = com.seeyon.ctp.common.AppContext.getBean("orgManager");
		
		def id = getValue(key, defaultValue, triggerDataContext);
		
		if(null == id){
			return defaultValue;
		}
		
		if(id instanceof java.util.List){
			
			def names = [];
			
			(0..<id.size()).each{i->
			
				def _names = [];
				
				try {
					
					id[i] = ((null != id[i]) ? (id[i] as String).split(',') : '');
					
					id[i].each{
						
						try {
							def unit = orgManager.getAccountById(new Long(it));
						
							_names << ((null != unit) ? unit.name : '');
						} catch(Exception e){}
					}
					
					names << _names.join(',');
				} catch(Exception e){
					names << '';
				}
			}
			
			return names;
		} else {
			
			def names = [];
			
			id = (id as String).split(',');
			
			id.each{
				
				def unit = orgManager.getAccountById(new Long(it));
				
				names << ((null != unit) ? unit.name : '');
			}
			
			return names.join(',');
		}
		
	} catch(Exception e){
		logger("从【${__IS_TRIGGER__ ? '触发器': '流程事件'}】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
	}
	
    return defaultValue;
}

/* 从【触发器/流程事件】的环境对象中获取表单数据，并转换为单位编号 */
def getUnitCode(key, defaultValue=null, triggerDataContext=__TRIGGER_CONTEXT__){

	try {

		def orgManager = com.seeyon.ctp.common.AppContext.getBean("orgManager");
		
		def id = getValue(key, defaultValue, triggerDataContext);
		
		if(null == id){
			return defaultValue;
		}
		
		if(id instanceof java.util.List){
			
			def names = [];
			
			(0..<id.size()).each{i->
			
				def _values = [];
				
				try {
					
					id[i] = ((null != id[i]) ? (id[i] as String).split(',') : '');
					
					id[i].each{
						
						try {
							def unit = orgManager.getAccountById(new Long(it));
						
							_values << ((null != unit) ? unit.code : '');
						} catch(Exception e){}
					}
					
					names << _values.join(',');
				} catch(Exception e){
					names << '';
				}
			}
			
			return names;
		} else {
			
			def names = [];
			
			id = (id as String).split(',');
			
			id.each{
				
				def unit = orgManager.getAccountById(new Long(it));
				
				names << ((null != unit) ? unit.code : '');
			}
			
			return names.join(',');
		}
		
	} catch(Exception e){
		logger("从【${__IS_TRIGGER__ ? '触发器': '流程事件'}】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
	}
	
    return defaultValue;
}

/* 从【触发器/流程事件】的环境对象中获取表单数据，并转换为岗位名称 */
def getPostName(key, defaultValue=null, triggerDataContext=__TRIGGER_CONTEXT__){

	try {

		def orgManager = com.seeyon.ctp.common.AppContext.getBean("orgManager");
		
		def id = getValue(key, defaultValue, triggerDataContext);
		
		if(null == id){
			return defaultValue;
		}
		
		if(id instanceof java.util.List){
			
			def names = [];
			
			(0..<id.size()).each{i->
			
				def _names = [];
				
				try {
					
					id[i] = ((null != id[i]) ? (id[i] as String).split(',') : '');
					
					id[i].each{
						
						try {
							def post = orgManager.getPostById(new Long(it));
						
							_names << ((null != post) ? post.name : '');
						} catch(Exception e){}
					}
					
					names << _names.join(',');
				} catch(Exception e){
					names << '';
				}
			}
			
			return names;
		} else {
			
			def names = [];
			
			id = (id as String).split(',');
			
			id.each{
				
				def post = orgManager.getPostById(new Long(it));
				
				names << ((null != post) ? post.name : '');
			}
			
			return names.join(',');
		}
		
	} catch(Exception e){
		logger("从【${__IS_TRIGGER__ ? '触发器': '流程事件'}】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
	}
	
    return defaultValue;
}

/* 从【触发器/流程事件】的环境对象中获取表单数据，并转换为岗位编号 */
def getPostCode(key, defaultValue=null, triggerDataContext=__TRIGGER_CONTEXT__){

	try {

		def orgManager = com.seeyon.ctp.common.AppContext.getBean("orgManager");
		
		def id = getValue(key, defaultValue, triggerDataContext);
		
		if(null == id){
			return defaultValue;
		}
		
		if(id instanceof java.util.List){
			
			def names = [];
			
			(0..<id.size()).each{i->
			
				def _values = [];
				
				try {
					
					id[i] = ((null != id[i]) ? (id[i] as String).split(',') : '');
					
					id[i].each{
						
						try {
							def post = orgManager.getPostById(new Long(it));
						
							_values << ((null != post) ? post.code : '');
						} catch(Exception e){}
					}
					
					names << _values.join(',');
				} catch(Exception e){
					names << '';
				}
			}
			
			return names;
		} else {
			
			def names = [];
			
			id = (id as String).split(',');
			
			id.each{
				
				def post = orgManager.getPostById(new Long(it));
				
				names << ((null != post) ? post.code : '');
			}
			
			return names.join(',');
		}
		
	} catch(Exception e){
		logger("从【${__IS_TRIGGER__ ? '触发器': '流程事件'}】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
	}
	
    return defaultValue;
}

/* 从【触发器/流程事件】的环境对象中获取表单数据，并转换为职务级别名称 */
def getLevelName(key, defaultValue=null, triggerDataContext=__TRIGGER_CONTEXT__){

	try {

		def orgManager = com.seeyon.ctp.common.AppContext.getBean("orgManager");
		
		def id = getValue(key, defaultValue, triggerDataContext);
		
		if(null == id){
			return defaultValue;
		}
		
		if(id instanceof java.util.List){
			
			def names = [];
			
			(0..<id.size()).each{i->
			
				def _names = [];
				
				try {
					
					id[i] = ((null != id[i]) ? (id[i] as String).split(',') : '');
					
					id[i].each{
						
						try {
							def level = orgManager.getLevelById(new Long(it));
						
							_names << ((null != level) ? level.name : '');
						} catch(Exception e){}
					}
					
					names << _names.join(',');
				} catch(Exception e){
					names << '';
				}
			}
			
			return names;
		} else {
			
			def names = [];
			
			id = (id as String).split(',');
			
			id.each{
				
				def level = orgManager.getLevelById(new Long(it));
				
				names << ((null != level) ? level.name : '');
			}
			
			return names.join(',');
		}
		
	} catch(Exception e){
		logger("从【${__IS_TRIGGER__ ? '触发器': '流程事件'}】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
	}
	
    return defaultValue;
}

/* 从【触发器/流程事件】的环境对象中获取表单数据，并转换为职务级别编号 */
def getLevelCode(key, defaultValue=null, triggerDataContext=__TRIGGER_CONTEXT__){

	try {

		def orgManager = com.seeyon.ctp.common.AppContext.getBean("orgManager");
		
		def id = getValue(key, defaultValue, triggerDataContext);
		
		if(null == id){
			return defaultValue;
		}
		
		if(id instanceof java.util.List){
			
			def names = [];
			
			(0..<id.size()).each{i->
			
				def _values = [];
				
				try {
					
					id[i] = ((null != id[i]) ? (id[i] as String).split(',') : '');
					
					id[i].each{
						
						try {
							def level = orgManager.getLevelById(new Long(it));
						
							_values << ((null != level) ? level.code : '');
						} catch(Exception e){}
					}
					
					names << _values.join(',');
				} catch(Exception e){
					names << '';
				}
			}
			
			return names;
		} else {
			
			def names = [];
			
			id = (id as String).split(',');
			
			id.each{
				
				def level = orgManager.getLevelById(new Long(it));
				
				names << ((null != level) ? level.code : '');
			}
			
			return names.join(',');
		}
		
	} catch(Exception e){
		logger("从【${__IS_TRIGGER__ ? '触发器': '流程事件'}】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
	}
	
    return defaultValue;
}


/* 从【触发器/流程事件】的环境对象中获取表单数据，并转换为数字 */
def getNumber(key, defaultValue=null, triggerDataContext=__TRIGGER_CONTEXT__){

	try {

		def value = getValue(key, defaultValue, triggerDataContext);
		
		if(null == value){
			return defaultValue;
		}
		
		if(value instanceof java.util.List){
			
			def values = [];
			
			value.each{val->
				
				if(null == val){
				
					values << null;
					
				}else if(val instanceof java.lang.Number){
				
					values << val;
				
				} else {
				
					values << new java.math.BigDecimal(val as String);
				}
			}
			
			return values;
		} else if(value instanceof java.lang.Number){
			
			return value;
			
		} else {
			
			return new java.math.BigDecimal(value as String);
		}
		
	} catch(Exception e){
		logger("从【${__IS_TRIGGER__ ? '触发器': '流程事件'}】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
	}
	
    return defaultValue;
}


/* 从【触发器/流程事件】的环境对象中获取表单数据，并转换为字符串 */
def getString(key, defaultValue=null, triggerDataContext=__TRIGGER_CONTEXT__){

	try {

		def value = getValue(key, defaultValue, triggerDataContext);
		
		if(null == value){
			return defaultValue;
		}
		
		if(value instanceof java.util.List){
			
			def values = [];
			
			value.each{val->
				
				if(null == val){
				
					values << null;
					
				}else if(val instanceof java.lang.String){
				
					values << val;
				
				} else {
				
					values << (val as String);
				}
			}
			
			return values;
		} else if(value instanceof java.lang.String){
			
			return value;
			
		} else {
			
			return value as String;
		}
		
	} catch(Exception e){
		logger("从【${__IS_TRIGGER__ ? '触发器': '流程事件'}】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
	}
	
    return defaultValue;
}


/* 从【触发器/流程事件】的环境对象中获取表单数据 */
def getValue(key, defaultValue=null, triggerDataContext=__TRIGGER_CONTEXT__){
    
	if(__IS_TRIGGER__) { // 在触发器环境下运行
		
		try {
	        def formData = triggerDataContext.getMasterBean();
	    
	        if(!(key ==~ /field\d+/)) {
	        	
	        	def formFieldBean = triggerDataContext.getFormBean().getFieldBeanByDisplay(key);
	           
	        	if(null != formFieldBean) {
	        		
	        		key = formFieldBean.getName();
	        	}
	        }
	        
	        def value = formData.getFieldValue(key);
	        
	        if(null == value){
	            value = formData.getAllDataMap().get(key);
	        }
	        
	        return (null != value ? value : defaultValue);
	    } catch(Exception e){
	        logger("从【触发器】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
	    }
	    
	}else { // 在流程事件环境下运行
		
		try {
	    	
	        def businessData = triggerDataContext.getBusinessData();
	        

	        def value = null;
	        
	        if(businessData.containsKey(key)) {
	        	
	        	value = businessData[key];
	        	
	        }else {

				def formDataBean = businessData['formDataBean'];
				def formBean = businessData['formBean'];
				
				if(!(key ==~ /field\d+/)) {
			        	
		        	def formFieldBean = formBean.getFieldBeanByDisplay(key);
		           
		        	if(null != formFieldBean) {
		        		
		        		key = formFieldBean.getName();
		        	}
		        }
		        
		        value = formDataBean.getFieldValue(key);
		        
		        if(null == value){
		            value = formDataBean.getAllDataMap().get(key);
		        }
	        }
	        
	        return (null != value ? value : defaultValue);
	    } catch(Exception e){
	        logger("从【流程事件】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
	    }
	}
    
    return defaultValue;
}


/* 在【触发器/流程事件】的环境下，对当前表单数据进行更新 */
def setValue(key, value, sleeptime=200, triggerDataContext=__TRIGGER_CONTEXT__){
	
	if(__IS_TRIGGER__) { // 在触发器环境下运行
		
		_setValueFromTrigger(key, value, sleeptime, triggerDataContext);
		
	}else { // 在流程事件环境下运行
		
		if(__TRIGGER_EVENT_TYPE__.startsWith("__ON_BEFORE_")) {
			
			_setValueFromWorkflow(key, value, triggerDataContext);
			
		}else {
			_setValueFromWorkflow2(key, value, sleeptime, triggerDataContext);
		}
	}
}

/* 在【流程事件】的环境下，对当前表单数据进行更新  (注：被更新字段不能为编辑态，因为编辑态下前端数据会覆盖后端数据)*/
def _setValueFromWorkflow(key, value, triggerDataContext=__TRIGGER_CONTEXT__){
		
	try {
		
		def businessData = triggerDataContext.getBusinessData();
		def formDataBean = businessData['formDataBean'];
		def formBean = businessData['formBean'];
    	def formFieldBean = formBean.getFieldBeanByDisplay(key);
    	
    	if(null == formFieldBean){
    		formFieldBean = formBean.getFieldBeanByName(key);
    	}
        
		if(null == formFieldBean){
			logger("在【流程事件】的环境下，${key}不存在！");
			return;
		}

		def fieldName = formFieldBean.getName();
		def tableName = formFieldBean.getOwnerTableName();
		
		def sql = '';
		def updataSize = 0;
		
		
		if(formFieldBean.isMasterField()) { // 主表
			
			formDataBean.addFieldValue(fieldName, ((value instanceof java.util.List && 0 < value.size()) ? value[0] : value));
			
		} else { // 从表
			        
			def subFormDataBeanList = formDataBean.getSubData(tableName);
			def subFormDataBeanListSize = subFormDataBeanList.size();
			
			if(subFormDataBeanListSize != value.size()) {
				
				logger("从表数据行数与传入的数据行数不匹配！【${subFormDataBeanListSize} -> ${value.size()}】");
			}
			
			subFormDataBeanListSize = (subFormDataBeanListSize < value.size() ? subFormDataBeanListSize : value.size());
			
			for(int i=0; i<subFormDataBeanListSize; i++){
				
				subFormDataBeanList[i].addFieldValue(fieldName, value[i]);
			}
		}
		
    } catch(Exception e){
        logger("在【流程事件】的环境下，对当前表单数据进行更新失败！[KEY: ${key}, ERROR: ${e.message}]");
    }
}


/* 在【流程事件】的环境下，对当前表单数据进行更新 (XXX后事件) */
def _setValueFromWorkflow2(key, value, sleeptime=200, triggerDataContext=__TRIGGER_CONTEXT__){
	
	
	try {
		
		def businessData = triggerDataContext.getBusinessData();    	
		def formData = businessData['formDataBean'];
    	def formBean = businessData['formBean'];
    	
    	def formFieldBean = formBean.getFieldBeanByDisplay(key);
    	
    	if(null == formFieldBean){
    		formFieldBean = formBean.getFieldBeanByName(key);
    	}
        
		if(null == formFieldBean){
			logger("在【触发器】的环境下，${key}不存在！");
			return;
		}

		
		def id = formData.getId();
		def fieldName = formFieldBean.getName();
		def tableName = formFieldBean.getOwnerTableName();
		
		def sql = ('UPDATE ' + tableName + ' SET ' + fieldName + '=? WHERE id=?');
		def dataList = [];
		
		if(formFieldBean.isMasterField()) { // 主表
			
			dataList << [((value instanceof java.util.List && 0 < value.size()) ? value[0] : value), id];
			
		} else { // 从表
			
			def subFormData = formData.getSubData(tableName);
        
			if(value.size() != subFormData.size()) {
				
				logger("从表数据行数与传入的数据行数不匹配！【${subFormData.size()} -> ${value.size()}】");
			}
			
			def subFormDataSize = (subFormData.size() < value.size() ? subFormData.size() : value.size());
			
			for(int i=0; i<subFormDataSize; i++){

				dataList << [value[i], subFormData[i].id];
			}
		}
		
		logger('TRIGGER_WORKFLOW_UPDATE_FORMDATA SQL >> ' + JSONHelper.stringify([sql, dataList]));
		
		Thread.start {

			// 延迟200毫秒
			sleep(sleeptime);
			
			def sqlClient = null;
			def sqlTxt = sql;
			def sqlDataList = dataList;
			
			try {
				
				// 执行SQL		
				sqlClient = getSQLClient();
				
				sqlClient.withTransaction{
					
					sqlDataList.each{_sqlData->
						
						def updataSize = sqlClient.executeUpdate(sql, _sqlData);
						
						if(1 != updataSize) {
							
							logger("在【流程事件】的环境下，对当前表单数据进行更新可能失败了！SQL: ${sqlTxt}, VALUE: ${_sqlData[0]}, ID: ${_sqlData[1]}");
						}
					}
				}
				
			}finally{
				
				if(null != sqlClient){
					try{
						sqlClient.close();
					} catch(Throwable e){}
				}
			}
			
		}
		
    } catch(Exception e){
        logger("在【流程事件】的环境下，对当前表单数据进行更新失败！[KEY: ${key}, ERROR: ${e.message}]");
    }
}


/* 在【触发器】的环境下，对当前表单数据进行更新 */
def _setValueFromTrigger(key, value, sleeptime=200, triggerDataContext=__TRIGGER_CONTEXT__){
	
	try {
		
		def formData = triggerDataContext.getMasterBean();
    	def formBean = triggerDataContext.getFormBean();
    	
    	def formFieldBean = formBean.getFieldBeanByDisplay(key);
    	
    	if(null == formFieldBean){
    		formFieldBean = formBean.getFieldBeanByName(key);
    	}
        
		if(null == formFieldBean){
			logger("在【触发器】的环境下，${key}不存在！");
			return;
		}

		
		def id = formData.getId();
		def fieldName = formFieldBean.getName();
		def tableName = formFieldBean.getOwnerTableName();
		
		def sql = ('UPDATE ' + tableName + ' SET ' + fieldName + '=? WHERE id=?');
		def dataList = [];
		
		if(formFieldBean.isMasterField()) { // 主表
			
			dataList << [((value instanceof java.util.List && 0 < value.size()) ? value[0] : value), id];
			
		} else { // 从表
			
			def subFormData = formData.getSubData(tableName);
        
			if(value.size() != subFormData.size()) {
				
				logger("从表数据行数与传入的数据行数不匹配！【${subFormData.size()} -> ${value.size()}】");
			}
			
			def subFormDataSize = (subFormData.size() < value.size() ? subFormData.size() : value.size());
			
			for(int i=0; i<subFormDataSize; i++){

				dataList << [value[i], subFormData[i].id];
			}
		}
		
		logger('TRIGGER_UPDATE_FORMDATA SQL >> ' + JSONHelper.stringify([sql, dataList]));
		
		Thread.start {
			
			// 延迟200毫秒
			sleep(sleeptime);
			
			def sqlClient = null;
			def sqlTxt = sql;
			def sqlDataList = dataList;
			
			try {
				
				// 执行SQL		
				sqlClient = getSQLClient();
				
				sqlClient.withTransaction{
					
					sqlDataList.each{_sqlData->
						
						def updataSize = sqlClient.executeUpdate(sqlTxt, _sqlData);
						
						if(1 != updataSize) {
							
							logger("在【触发器】的环境下，对当前表单数据进行更新可能失败了！SQL: ${sqlTxt}, VALUE: ${_sqlData[0]}, ID: ${_sqlData[1]}");
						}
					}
				}
				
			}finally{
				
				if(null != sqlClient){
					try{
						sqlClient.close();
					} catch(Throwable e){}
				}
			}
			
		}
		
    } catch(Exception e){
        logger("在【触发器】的环境下，对当前表单数据进行更新失败！[KEY: ${key}, ERROR: ${e.message}]");
    }
}




